import { YY_STS_AR } from "./YYStsAr";
import { YY_STS_EN } from "./YYStsEn";
import { YY_STS_ES } from "./YYStsEs";
import { YY_STS_HI } from "./YYStsHi";
import { YY_STS_ID } from "./YYStsId";
import { YY_STS_JP } from "./YYStsJp";
import { YY_STS_MS } from "./YYStsMs";
import { YY_STS_MY } from "./YYStsMy";
import { YY_STS_PT } from "./YYStsPt";
import { YY_STS_RU } from "./YYStsRu";
import { YY_STS_TH } from "./YYStsTh";
import { YY_STS_VI } from "./YYStsVi";
import { YY_STS_ZHF } from "./YYStsZhf";

interface JsonObj {
    [key: string]: any
};
const { ccclass, property } = cc._decorator;

@ccclass
export default class YYSystemLanguage extends cc.Component {

    private static _hasInit: boolean = false;

    // LIFE-CYCLE CALLBACKS:

    /** 组件如启用，默认初始化语言 */
    protected onLoad(): void {
        YYSystemLanguage.init();
    }

    /** 静态方法, 可供直接调用 */
    static init() {
        if (this._hasInit) return;
        const lanJson: JsonObj = {
            "zhf": YY_STS_ZHF,
            "en": YY_STS_EN,
            "id": YY_STS_ID,
            "hi": YY_STS_HI,
            "th": YY_STS_TH,
            "vi": YY_STS_VI,
            "ar": YY_STS_AR,
            "ms": YY_STS_MS,
            "pt": YY_STS_PT,
            "es": YY_STS_ES,
            "ru": YY_STS_RU,
            "jp": YY_STS_JP,
            "my": YY_STS_MY,
        };
        for (const key in lanJson) {
            if (key === undefined) continue;
            if (CC_PREVIEW) console.info("开始加载框架多语言:", key);
            this.setlanguage(key, lanJson[key]);
        }
        this.languageChanged();
        yy.setCurrentCountry(yy.curLanguage);
        this._hasInit = true;
    }

    private static setlanguage(lan: string, moduleObj: any) {
        const lanText = yy.languageCenter.getTextCenter(lan);
        for (const moduleKey in moduleObj) {
            if (moduleKey === undefined || moduleObj[moduleKey] === undefined) continue;
            const idx = parseInt(moduleKey);
            const obj = moduleObj[moduleKey];
            for (const key in obj) {
                if (key === undefined) continue;
                lanText.setText(idx, parseInt(key), obj[key]);
            }
        }
    }

    // 特定渠道指定的多语言内容调整
    private static languageChanged() {
        const pkgid = yy.lobby.getPackageID();
        if (pkgid == "guochao") {
            const ZH_FIX = {
                "14": {
                    "2": "网络状况不佳，请重试。"
                }
            }
            this.setlanguage("zh", ZH_FIX);

            const ZHF_FIX = {
                "14": {
                    "2": "網絡狀況不佳，請重試。"
                }
            }
            this.setlanguage("zhf", ZHF_FIX);
        }
    }

}
